﻿<script>
	function Back(){
        window.history.back();
    }
</script>
<?php
include_once('database.php');
if(isset($_GET["id"])){
	$query ="SELECT Name, MediaId, ReleaseDate, MaterialId FROM item WHERE MaterialId=".$_GET["id"];
	$stmt = $db -> prepare($query); 
	$stmt -> execute();
    $stmt -> store_result();
    $stmt -> bind_result($name,$mediaid,$releasedate,$id);
    $stmt -> fetch();
    $exp = explode('-', $releasedate);
	$year = $exp[0];
	$month = $exp[1];
	$day = $exp[2];
}
?>
<table>
	<tr>
		<td><b>Navn:</b></td>										
		<td><input type="text" name="Name" value="<?php echo session_check('Material','Name'); if(session_check('Material','Name')==false&&isset($name)){echo htmlspecialchars($name);}?>"/></td>			
	</tr>
    <tr>
		<td><b>Medie:</b></td>										
		<td>        	
			<?php 
                $media=$db->query("SELECT * FROM media;");
				if($media -> num_rows == 0){
					echo 	'<a href="media.php">
								Ingen medier. Opret nogle.
							</a>';
				}else{
					echo '<select name="Media">';
					while($result = $media->fetch_assoc()){
						echo 	'<option value="'.$result["MediaId"].'" ';
                            echo session_check('Material','Media')==$result["MediaId"]? "Selected" : isset($mediaid)&&$mediaid==$result["MediaId"]? "Selected":"";
						echo 	'>'.htmlspecialchars($result["MediaName"]).'
								</option>';

					}
					echo '</select>';
					
				}
				?>
		</td>			
	</tr>
    <tr>
		<td><b>Genre:</b></td>
		<?php 
			$genres=$db->query("SELECT * FROM genre;");

			if($genres ->num_rows == 0){
				echo 	'<td>
							<a href="genre.php">
								Ingen genre. Opret nogle.
							</a>		
						</td>
					</tr>';
			}else{
				echo '</tr>';
				
				while($result = $genres->fetch_assoc()){
					echo '<tr>
							<td>
								'.htmlspecialchars($result["GenreName"]).'
								
							</td>
							<td>
								<input type="checkbox" name="GenreName[]" value="'.$result["GenreId"].'" ';
									if(session_check('Material','GenreName')!=false){
    									foreach(session_check('Material','GenreName') as $value){
    										if($value==$result["GenreId"]){
    											echo 'checked="checked"';
    										}
    									}
									}
                                    else if(isset($_GET["id"])){         
                                        $query= $db->query("SELECT itemgenre.GenreId 
                                                                FROM itemgenre,genre 
                                                                WHERE itemgenre.MaterialId=".$_GET["id"]."
                                                                 AND itemgenre.genreid =".$result["GenreId"]." 
                                                                 AND itemgenre.genreid = genre.genreid;");
                                        $checked =$query->fetch_assoc();
                                        if($checked["GenreId"]==$result["GenreId"]){
                                            
                                            echo 'checked="checked"';               
                                        }
                                    }
					echo 		'>			
							</td>
						</tr>';
				}
				
			}
		 ?>
</table>
<table>
	<tr>
		<td><b>Release: År</b></td>
		<td></td>
		<td><b>Måned</b></td>
		<td></td>
		<td><b>Dag</b></td>
	</tr>
	<tr>
		<td><input type="text" pattern="[0-9]*" name="Year" placeholder="År" maxlength="4" value="<?php echo session_check('Material','Year');if(session_check('Material','Year')==false&&isset($year)){echo $year;}?>"></td>
		<td>-</td>
		<td>
			<select name="Month" >
				<?php
                    for($v=1;$v<=12;$v++){
                        echo '<option ';
                            echo session_check('Material','Month')==$v? "Selected" : isset($month)&&$month==$v? "Selected":"";
                        echo '>'.$v.'</option>';
                    }
				?>
            </select>
		</td>
        <td>-</td>
        <td>
			<select name="Day">
				<?php
					for($v=1;$v<=31;$v++){
                        echo '<option ';
                            echo session_check('Material','Day')==$v? "Selected" : isset($day)&&$day==$v? "Selected":"";
						echo '>'.$v.'</option>';
					}
				?>
			</select>
        </td>
	</tr>
</table>
<b>Picture:</b>
<br />
Søg din computer:
<br />
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input type="file" name="local_picture"/>
<br />
Eller vælg et billede fra nettet:
<br />
<input type="url" name="net_picture" value="<?php echo session_check('Material','net_picture')?>"/>
<br />
<input type="submit" id="submit" value="OK"/>


<?php unset($_SESSION["Material"]) ?>
